package com.september.mongo.entities;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Key;
import com.google.code.morphia.annotations.Embedded;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.september.mongo.data.DataProvider;
import com.september.util.BaseData;
@Entity
public class User extends BaseData {
	
	@Id
	private String userName;
	private boolean active;
	@Embedded
	private PersonSpec spec;
	
	private String name;
	private String surname;
	private String prevCode;
	private Properties afterProp;
	//private final String SALT = ""+UUID.randomUUID();
	//private String digest;
	private boolean master;
	@Embedded
	private Map<String,Object> extraParams;
	
	public boolean isMaster() {
		return master;
	}
	

	public static boolean login(String user, String password) {
		 Datastore ds = (new DataProvider()).getDatastore();
		 //Key<User> key = new Key<>(User.class, user);
		 //User userBean = ds.find(User.class).field("_id").equal(user).get();
		 Salt salt = ds.find(Salt.class).field("_id").equal(user).get();
		 try {
			 return salt.getDigest().equals(salt.calculateDigest(password));
		 }
		 catch (Exception e) {
			 e.printStackTrace();
			 return false;
		 }
		
	}
	
	public  User signup(String user,String password) {
		this.userName=user;
		
		try {
		
		 Datastore ds = (new DataProvider()).getDatastore();
		 Key<User> key = new Key<User>(User.class, this.userName);
		 if(ds.exists(key)  != null) {
			 return null;
		 }
		 else {
			 Salt salt = new Salt(user, password);
			 ds.save(salt);
			 ds.save(this);
			 return this;
		 }
		 
		 }
		
		catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	} 
	
	public Properties getAfterProp() {
		return afterProp;
	}

	public void setAfterProp(Properties afterProp) {
		this.afterProp = afterProp;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getPrevCode() {
		return prevCode;
	}

	public void setPrevCode(String prevCode) {
		this.prevCode = prevCode;
	}

	public Map<String, Object> getExtraParams() {
		return extraParams;
	}

	public void setExtraParams(Map<String, Object> extraParams) {
		this.extraParams = extraParams;
	}
	
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public PersonSpec getSpec() {
		return spec;
	}

	public void setSpec(PersonSpec spec) {
		this.spec = spec;
	}

	@Override
	public String[] getOrdreredField() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void manageRefernces() {
		// TODO Auto-generated method stub

	}

}
